package com.softstao.guoyu.ui.activity.sale;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.global.UserManager;
import com.softstao.guoyu.model.sale.SellByYears;
import com.softstao.guoyu.mvp.interactor.sale.RetailInteractor;
import com.softstao.guoyu.mvp.presenter.sale.RetailPresenter;
import com.softstao.guoyu.mvp.viewer.sale.SaleByYearsViewer;
import com.softstao.guoyu.ui.activity.agent.AboutOrderActivity;
import com.softstao.guoyu.ui.activity.me.RebateActivity;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2018/5/5.
 */

public class SaleRebateActivity extends BaseActivity implements SaleByYearsViewer {

    @AIPresenter(presenter = RetailPresenter.class, interactor = RetailInteractor.class)
    RetailPresenter presenter;

    @BindView(R.id.month_money)
    TextView monthMoney;
    @BindView(R.id.time)
    TextView time;
//    @BindView(R.id.year_money)
//    TextView yearMoney;
    @BindView(R.id.date_title)
    TextView dateTitle;
    @BindView(R.id.one_level_rebate)
    TextView oneLevelRebate;
    @BindView(R.id.one_level_rebate_view)
    LinearLayout oneLevelRebateView;
    @BindView(R.id.two_standard)
    TextView twoStandard;
    @BindView(R.id.two_level_rebate)
    TextView twoLevelRebate;
    @BindView(R.id.two_level_rebate_view)
    LinearLayout twoLevelRebateView;
    @BindView(R.id.month_rebate)
    TextView monthRebate;
    @BindView(R.id.month_rebate_view)
    LinearLayout monthRebateView;
    @BindView(R.id.my_sell)
    TextView mySell;
    @BindView(R.id.my_sell_view)
    LinearLayout mySellView;
//    @BindView(R.id.estimate_money)
//    TextView estimateMoney;
//    @BindView(R.id.estimate_money_view)
//    LinearLayout estimateMoneyView;
    @BindView(R.id.month_manage_rebate)
    TextView monthManageRebate;
    @BindView(R.id.month_standard)
    TextView monthStandard;
    @BindView(R.id.month_manage_rebate_view)
    LinearLayout monthManageRebateView;
    @BindView(R.id.years_money)
    TextView yearsMoney;
    @BindView(R.id.years_money_view)
    LinearLayout yearsMoneyView;
    @BindView(R.id.loading)
    RelativeLayout loading;

    private Calendar calendar = Calendar.getInstance();
    private int year, month;
    private int level;
    private SellByYears sellByYears;

    @Override
    public int _ContentView() {
        return R.layout.activity_sale_rebate;
    }

    @Override
    public void initView() {
        initTitle("我的收入");
        level = UserManager.getInstance().getUser().getLevel();
        if (level == 5) {
            oneLevelRebateView.setVisibility(View.VISIBLE);
            twoLevelRebateView.setVisibility(View.VISIBLE);
            monthRebateView.setVisibility(View.GONE);
//            estimateMoneyView.setVisibility(View.VISIBLE);
            monthManageRebateView.setVisibility(View.VISIBLE);
            yearsMoneyView.setVisibility(View.VISIBLE);
        } else {
            oneLevelRebateView.setVisibility(View.GONE);
            twoLevelRebateView.setVisibility(View.GONE);
            monthRebateView.setVisibility(View.VISIBLE);
//            estimateMoneyView.setVisibility(View.GONE);
            monthManageRebateView.setVisibility(View.GONE);
            yearsMoneyView.setVisibility(View.GONE);
        }

        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        time.setText(year + "年" + month + "月");
    }

    private void initData(){
        monthMoney.setText(sellByYears.getMonthsMenoy() + "元");
//        yearMoney.setText("年收入：" + sellByYears.getYearsMenoy() + "元");
        mySell.setText(sellByYears.getMySell() + "元");
        if (level==5){
            oneLevelRebate.setText(sellByYears.getOneLevelRebate() + "元");
            twoLevelRebate.setText(sellByYears.getTwoLevelRebate() + "元");
//            estimateMoney.setText(sellByYears.getEstimateMenoy() + "元");
            if (sellByYears.getTwoIstandard()==0){
                twoStandard.setText("不达标");
                twoStandard.setTextColor(getResources().getColor(R.color.font_green));
                twoStandard.setBackground(getResources().getDrawable(R.drawable.green_border_line));
            }else {
                twoStandard.setText("达标");
                twoStandard.setTextColor(getResources().getColor(R.color.font_pink));
                twoStandard.setBackground(getResources().getDrawable(R.drawable.pink_border_line));
            }
            monthManageRebate.setText(sellByYears.getMonthManageRebate() + "元");
            if (sellByYears.getMonthIstandard()==0){
                monthStandard.setText("不达标");
                monthStandard.setTextColor(getResources().getColor(R.color.font_green));
                monthStandard.setBackground(getResources().getDrawable(R.drawable.green_border_line));
            }else {
                monthStandard.setText("达标");
                monthStandard.setTextColor(getResources().getColor(R.color.font_pink));
                monthStandard.setBackground(getResources().getDrawable(R.drawable.pink_border_line));
            }
            yearsMoney.setText(sellByYears.getYearsMenoy() + "元");
        }else {
            monthRebate.setText(sellByYears.getMonthsRebate() + "元");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        findSaleByYears();
    }

    @OnClick({R.id.left, R.id.right, R.id.one_level_rebate_view, R.id.two_level_rebate_view,
            R.id.month_rebate_view, R.id.my_sell_view, R.id.month_manage_rebate_view, R.id.years_money_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left:
                month--;
                if (month < 1) {
                    year--;
                    month = 12;
                }
                time.setText(year + "年" + month + "月");
                dateTitle.setText(time.getText() + "收入明细");
                findSaleByYears();
                break;
            case R.id.right:
                month++;
                if (month > 12) {
                    year++;
                    month = 1;
                }
                time.setText(year + "年" + month + "月");
                dateTitle.setText(time.getText() + "收入明细");
                findSaleByYears();
                break;
            case R.id.one_level_rebate_view:
                Intent oneIntent = new Intent(context, ProvinceRebateDetailsActivity.class);
                oneIntent.putExtra("method", "oneLevel");
                oneIntent.putExtra("time", time.getText());
                startActivity(oneIntent);
                break;
            case R.id.two_level_rebate_view:
                Intent twoIntent = new Intent(context, ProvinceRebateDetailsActivity.class);
                twoIntent.putExtra("method", "twoLevel");
                twoIntent.putExtra("time", time.getText());
                startActivity(twoIntent);
                break;
            case R.id.month_rebate_view:
                startActivity(new Intent(context, RebateActivity.class));
                break;
            case R.id.my_sell_view:
                Intent sellIntent = new Intent(context, AboutOrderActivity.class);
                sellIntent.putExtra("type", "user");
                sellIntent.putExtra("method", 1);
                sellIntent.putExtra("year", year);
                sellIntent.putExtra("month", month);
                startActivity(sellIntent);
                break;
            case R.id.month_manage_rebate_view:
                Intent monthIntent = new Intent(context, ProvinceRebateDetailsActivity.class);
                monthIntent.putExtra("method", "monthManage");
                monthIntent.putExtra("time", time.getText());
                startActivity(monthIntent);
                break;
            case R.id.years_money_view:
                Intent yearIntent = new Intent(context, YearsManageActivity.class);
                yearIntent.putExtra("time", time.getText());
                startActivity(yearIntent);
                break;
        }
    }

    @Override
    public void findSaleByYears() {
        loading.setVisibility(View.VISIBLE);
        presenter.getSaleByYears(SharePreferenceManager.getInstance().getAgentId(), time.getText().toString().replace("年", "-").replace("月", ""));
    }

    @Override
    public void getSaleByYears(SellByYears sellByYears) {
        loading.setVisibility(View.GONE);
        if (sellByYears != null) {
            this.sellByYears = sellByYears;
            initData();
        }
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
